package com.example.bankend.controllers;

import com.example.bankend.entity.Card;
import com.example.bankend.form.*;
import com.example.bankend.param.Result;
import com.example.bankend.service.CardService;
import com.example.bankend.util.Validator;
import com.example.bankend.vo.CardResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author luolinila
 * @date 2025/6/18 09:44
 */
@RestController
@CrossOrigin
public class CardController {
    @Autowired
    private CardService cardService;

    @PostMapping("/card/create")
    public Result<CardResult> createCard(@RequestBody CreateCard form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        String cardID = cardService.createCard(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getPass(), form.getSavingName(), form.getPID());
        if ("10004".equals(cardID)) {
            return new Result<>(10004, "存款类型不存在", null);
        }
        if ("10005".equals(cardID)) {
            return new Result<>(10005, "用户不存在", null);
        }
        if ("-1".equals(cardID)) {
            return new Result<>(10006, "添加失败", null);
        }
        Card card = cardService.getCardByID(cardID);
        if (card == null) {
            return new Result<>(10007, "未知错误", null);
        }
        System.out.println(card);
        CardResult res = cardService.CardEncapsulaReturns(card);
        if (res == null) {
            return new Result<>(10007, "未知错误", null);
        }
        return new Result<>(200, "添加成功", res);
    }

    @PostMapping("/card/delete")
    public Result<CardResult> deleteCard(@RequestBody DeleteCard form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        int res = cardService.deleteCard(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCardID());
        if (res == -1) {
            return new Result<>(10001, "删除失败", null);
        }
        if (res == 10001) {
            return new Result<>(10002, "银行卡不存在", null);
        }
        return new Result<>(200, "删除成功", null);
    }

    @PostMapping("/card/chancePass")
    public Result<CardResult> chancePass(@RequestBody ChangeCardPass form) {
        boolean isTure = form.isTure();
        if (!isTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getNewPass());
        if (!isPass) {
            return new Result<>(10004, "密码格式错误", null);
        }
        Long res = cardService.changeCardPass(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCardID(), form.getNewPass());
        if (res == -1) {
            return new Result<>(10005, "修改失败", null);
        }
        if (res == 10001) {
            return new Result<>(10006, "银行卡不存在", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10010, "未知错误", null);
        }
        CardResult resCard = cardService.CardEncapsulaReturns(card);
        if (resCard == null) {
            return new Result<>(10010, "未知错误", null);
        }
        return new Result<>(200, "修改成功", resCard);
    }

    @PostMapping("/card/handleLoss")
    public Result<CardResult> handleLoss(@RequestBody HandleLoss form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getPass());
        if (!isPass) {
            return new Result<>(10004, "密码格式错误", null);
        }
        int res = cardService.handelLoss(form.getCardID(), form.getPass());
        if (res == -1) {
            return new Result<>(10005, "挂失失败", null);
        }
        if (res == 10001) {
            return new Result<>(10006, "银行卡不存在", null);
        }
        if (res == 10003) {
            return new Result<>(10008, "密码错误", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10010, "未知错误", null);
        }
        CardResult resCard = cardService.CardEncapsulaReturns(card);
        if (resCard == null) {
            return new Result<>(10010, "未知错误", null);
        }
        return new Result<>(200 ,"挂失成功", resCard);
    }

    @PostMapping("/card/restoreUse")
    public Result<CardResult> restoreUse(@RequestBody RestoreUse form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        Long res = cardService.restoreUse(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCardID());
        if (res == -1) {
            return new Result<>(10005, "恢复失败", null);
        }
        if (res == 10001) {
            return new Result<>(10006, "银行卡不存在", null);
        }
        if (res == 10002) {
            return new Result<>(10007, "客户不存在", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10010, "未知错误", null);
        }
        CardResult resCard = cardService.CardEncapsulaReturns(card);
        if (resCard == null) {
            return new Result<>(10010, "未知错误", null);
        }
        return new Result<>(200 ,"恢复成功", resCard);
    }

    @PostMapping("/card/selectAllCard")
    public Result<List<CardResult>>  selectAllCard(@RequestBody SelectAll form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<Card> res = cardService.selectAllCard(form.getAdminID(), form.getAdminName(), form.getAdminPass());
        if (res == null) {
            return new Result<>(10001, "查询失败", null);
        }
        List<CardResult> resCards = new ArrayList<>();
        for (Card card: res) {
            resCards.add(cardService.CardEncapsulaReturns(card));
        }
        return new Result<>(200, "查询成功", resCards);
    }

    @PostMapping("/card/getAllCardByID")
    public Result<List<CardResult>> getAllCardByID(@RequestBody SelectAllCard form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<Card> cards = cardService.getAllCardByID(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerID());
        if (cards == null) {
            return new Result(10001, "未知错误", null);
        }
        List<CardResult> resCards =  new ArrayList<>();
        for (Card card : cards) {
            resCards.add(cardService.CardEncapsulaReturns(card));
        }
        return new Result<>(200, "获取成功", resCards);
    }

    @PostMapping("/card/selectBalance")
    public Result<CardResult> selecrBalance(@RequestBody SelectBalance form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getPass());
        if (!isPass) {
            return new Result<>(10004, "密码格式错误", null);
        }
        String id = cardService.selecrBalance(form.getCardID(), form.getPass());
        if ("10004".equals(id) || "10005".equals(id)) {
            return new Result<>(10003, "密码错误", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10006, "未知错误", null);
        }
        CardResult res = cardService.CardEncapsulaReturns(card);
        if (res == null) {
            return new Result<>(10006, "未知错误", null);
        }
        return new Result<>(200, "查询成功", res);
    }

    @PostMapping("/card/updatePass")
    public Result<CardResult> updatePass(@RequestBody UpdatePass form) {
        boolean isTure = form.isTrue();
        if (!isTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getLastPass()) && Validator.isCardPass(form.getNewPass());
        if (!isPass) {
            return new Result<>(10004, "密码格式错误", null);
        }
        String res = cardService.updatePass(form.getCardID(), form.getLastPass(), form.getNewPass());
        if ("-1".equals(res)) {
            return new Result<>(10005, "修改失败", null);
        }
        if ("10001".equals(res)) {
            return new Result<>(10006, "银行卡不存在", null);
        }
        if ("10002".equals(res)) {
            return new Result<>(10007, "密码错误", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10008, "未知错误", null);
        }
        CardResult resCard = cardService.CardEncapsulaReturns(card);
        if (resCard == null) {
            return new Result<>(10008, "未知错误", null);
        }
        return new Result<>(200, "修改成功", resCard);
    }
}
